<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="所属部门" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sysOrgCode">
              <j-select-depart v-model="model.sysOrgCode" customReturnField="org_code"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="员工姓名" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="acUserid">
              <!-- <a-input v-model="model.acName" placeholder="请输入员工姓名"  ></a-input> -->
              <j-select-user-by-dep v-model="model.acUserid" :multi="false" customReturnField="id"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="资产授权" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="lastUseInfo">
              <!-- <j-select-role v-model="model.lastUseInfo" @change="changeMe"/> -->
              <j-select-assets queryParamCode="username" v-model="model.lastUseInfo" @change="changeMe"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="开始日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="startDate">
              <j-date placeholder="请选择开始日期" v-model="model.startDate" :show-time="false" date-format="YYYY-MM-DD"
                      style="width: 100%"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="结束日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="endDate">
              <j-date placeholder="请选择结束日期" v-model="model.endDate" :show-time="false" date-format="YYYY-MM-DD"
                      style="width: 100%"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
              <a-input v-model="model.remark" placeholder="请输入备注"></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

import {getAction, httpAction} from '@/api/manage'

export default {
  name: 'AsAccountForm',
  components: {},
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      model: {
        acStatus: "1",
        acUserid: this.acUserid
      },
      labelCol: {
        xs: {span: 24},
        sm: {span: 5},
      },
      wrapperCol: {
        xs: {span: 24},
        sm: {span: 16},
      },
      confirmLoading: false,
      validatorRules: {
        acStatus: [{required: true, message: '请输入员工状态!'}],
        acUserid: [
          {required: true, message: '请输入员工姓名!'},
          {validator: this.checkUser}
        ],
        lastUseInfo: [
          {required: true, message: '请输入资产!'},
          {validator: this.checkAccount}
        ],
        startDate: [
          {required: true, message: '请输入开始日期!'},
        ],
        endDate: [{required: true, message: '请输入结束日期!'}],
      },
      url: {
        add: "/assets/asAccount/add",
        edit: "/assets/asAccount/edit",
        queryById: "/assets/asAccount/queryById",
        checkAccount: "/assets/asAccount/checkAccount",
      },
      // 选择用户查询条件配置
      selectUserQueryConfig: [
        {key: 'phone', label: '电话'},
      ],
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    },
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model));
  },
  methods: {
    checkUser(rule, value, callback) {
      if (this.model.queryType===0){
        callback();
        return ;
      }
      this.model.queryType = 1;
      getAction(this.url.checkAccount, this.model).then((res) => {
        if (!res.result) {
          callback();
        } else {
          var that = this;
          this.$confirm({
            title: '确认选择',
            content: res.result,
            onOk() {
              callback();
            },
            onCancel() {
              that.loading = true;
              that.model.acUserid = null;
              callback();
            },
          });
        }
      })
    },
    checkAccount(rule, value, callback) {
      if (this.model.queryType===0){
        callback();
        return ;
      }
      this.model.queryType = 2;
      getAction(this.url.checkAccount, this.model).then((res) => {
        if (res.result == null) {
          callback();
        } else {
          var that = this;
          this.$confirm({
            title: '确认关联',
            content: res.result,
            onOk() {
              callback();
            },
            onCancel() {
              that.loading = true;
              that.model.lastUseInfo = null;
              callback();
            },
          });
        }
      })
    },
    add() {
      this.edit(this.modelDefault);
    },
    edit(record) {
      this.model = Object.assign({}, record);
      this.visible = true;
    },
    submitForm() {
      this.model.queryType = 0;
      const that = this;
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true;
          let httpurl = '';
          let method = '';
          if (!this.model.id) {
            httpurl += this.url.add;
            method = 'post';
          } else {
            httpurl += this.url.edit;
            method = 'put';
          }
          httpAction(httpurl, this.model, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message);
              that.$emit('ok');
            } else {
              that.$message.warning(res.message);
            }
          }).finally(() => {
            that.confirmLoading = false;
          })
        }

      })
    },
    changeMe() {
      console.log('you so ...  , change Me')
    },

  }
}
</script>